package com.org.daniel.project.controller.manager;

import com.org.daniel.module.domain.ResultResponse;
import com.org.daniel.module.domain.vo.manager.LoginVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author Daniel
 */
@Controller
@RequestMapping("manager")
public class LoginController {
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(Model model){
		return "login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse login(@RequestBody LoginVO loginVO){
		logger.info("manager login param={}", loginVO.toString());
		ResultResponse resp = new ResultResponse();
		UsernamePasswordToken token = new UsernamePasswordToken(
				loginVO.getUsername(), loginVO.getPassword(), false);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			resp.setResultCode("0");
			resp.setResultMessage("success");
		}
		catch (Exception e){
			resp.setResultCode("1");
			resp.setResultMessage("failure");
		}
		return resp;
	}

}
